package top.kaoshanji.p1ch02d;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * 代码清单 p1ch02d-05：MethodHandles#insertArguments的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 18:26
 */
public class MethodHandlesExample05 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesExample05.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02d-05：MethodHandles#insertArguments的使用示例...");

        MethodType type = MethodType.methodType(String.class, String.class);
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodHandle mhOld = lookup.findVirtual(String.class, "concat", type);
        String value = (String) mhOld.invoke("Hello", "World");
        System.out.println(value);

        MethodHandle mhNew = MethodHandles.insertArguments(mhOld, 1, " --");
        value = (String) mhNew.invoke("Hello");
        System.out.println(value);

    }

}
